{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T08:34:24.497499Z",
     "start_time": "2025-10-24T08:34:24.488631Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df1 = pd.DataFrame([['李明','M',18,'北京'],\n",
    "                    ['张华','M',19,'天津'],\n",
    "                    ['刘涛','F',20,'上海'],\n",
    "                    ['王阳','F',14,'广州'],\n",
    "                    ['李春','F',16,'深圳']],\n",
    "                    index=['one', 'two', 'three', 'four','five'],\n",
    "                    columns=['name','gender','age','city'])\n",
    "print(df1)"
   ],
   "id": "823fed3096dd0634",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name gender  age city\n",
      "one     李明      M   18   北京\n",
      "two     张华      M   19   天津\n",
      "three   刘涛      F   20   上海\n",
      "four    王阳      F   14   广州\n",
      "five    李春      F   16   深圳\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T08:38:11.668736Z",
     "start_time": "2025-10-24T08:38:11.617244Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data=df1[['name']][:3]\n",
    "print('访问name列的前3行数据：\"')\n",
    "display(data)\n",
    "data=df1[['name','age']][:3]\n",
    "print('访问name列和age列的前3行数据：\"')\n",
    "display(data)\n",
    "data=df1[1:3]\n",
    "print('访问低1-2行的数据：“')\n",
    "display(data)"
   ],
   "id": "2f2d1d43524a66d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "访问name列的前3行数据：\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "      name\n",
       "one     李明\n",
       "two     张华\n",
       "three   刘涛"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>李明</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>张华</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>刘涛</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data",
     "jetTransient": {
      "display_id": null
     }
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "访问name列和age列的前3行数据：\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "      name  age\n",
       "one     李明   18\n",
       "two     张华   19\n",
       "three   刘涛   20"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>李明</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>张华</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>刘涛</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data",
     "jetTransient": {
      "display_id": null
     }
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "访问低1-2行的数据：“\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "      name gender  age city\n",
       "two     张华      M   19   天津\n",
       "three   刘涛      F   20   上海"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>gender</th>\n",
       "      <th>age</th>\n",
       "      <th>city</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>张华</td>\n",
       "      <td>M</td>\n",
       "      <td>19</td>\n",
       "      <td>天津</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>刘涛</td>\n",
       "      <td>F</td>\n",
       "      <td>20</td>\n",
       "      <td>上海</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data",
     "jetTransient": {
      "display_id": null
     }
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T08:43:12.211853Z",
     "start_time": "2025-10-24T08:43:12.203685Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data=df1.loc['three',['name','city']]\n",
    "print('获取第3行name列和city列数据的结果:\\n',data)"
   ],
   "id": "5033e21e61f61041",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取第3行name列和city列数据的结果:\n",
      " name    刘涛\n",
      "city    上海\n",
      "Name: three, dtype: object\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T09:18:06.809984Z",
     "start_time": "2025-10-24T09:18:06.803560Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data=df1.iloc[2]\n",
    "print('获取第二行的数据结果\\n',data)"
   ],
   "id": "f14d756fba069ab2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取第二行的数据结果\n",
      " name      刘涛\n",
      "gender     F\n",
      "age       20\n",
      "city      上海\n",
      "Name: three, dtype: object\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T09:20:06.481970Z",
     "start_time": "2025-10-24T09:20:06.471937Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data=df1.iloc[2,[3,0,1]]\n",
    "print('获取第二行中第3，0，1列数据的结果\\n',data)"
   ],
   "id": "3608239d9e6b6018",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取第二行中第3，0，1列数据的结果\n",
      " city      上海\n",
      "name      刘涛\n",
      "gender     F\n",
      "Name: three, dtype: object\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T09:22:18.914792Z",
     "start_time": "2025-10-24T09:22:18.907317Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data=df1.iloc[[1,2],[3,0,1]]\n",
    "print(data)"
   ],
   "id": "77b620da8a36731d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      city name gender\n",
      "two     天津   张华      M\n",
      "three   上海   刘涛      F\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-24T09:29:48.066047Z",
     "start_time": "2025-10-24T09:29:48.056015Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data=df1.iloc[:,:3][df1.age>18]\n",
    "print('获取第三行中C列大于18的结果\\n',data)"
   ],
   "id": "81e8cb3f3cef2806",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取第三行中C列大于18的结果\n",
      "       name gender  age\n",
      "two     张华      M   19\n",
      "three   刘涛      F   20\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "45ffd00e3b2b8dda"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
